package com.linkhub.oms.admin.modules.logistic.freighttemplate.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.linkhub.oms.admin.modules.logistic.freighttemplate.domain.param.LogisticFreightTemplateAddParam;
import com.linkhub.oms.admin.modules.logistic.freighttemplate.domain.param.LogisticFreightTemplateQueryParam;
import com.linkhub.oms.admin.modules.logistic.freighttemplate.domain.param.LogisticFreightTemplateUpdParam;
import com.linkhub.oms.admin.modules.logistic.freighttemplate.domain.vo.LogisticFreightTemplateDetailVO;
import com.linkhub.oms.admin.modules.logistic.freighttemplate.domain.vo.LogisticFreightTemplateExcelVO;
import com.linkhub.oms.admin.modules.logistic.freighttemplate.domain.vo.LogisticFreightTemplateVO;
import com.linkhub.oms.admin.modules.logistic.freighttemplate.service.LogisticFreightTemplateBsService;
import com.linkhub.oms.admin.modules.satoken.session.LhSessionContext;
import com.linkhub.oms.common.constant.RoleConstant;
import com.linkhub.oms.common.response.DataResponse;
import com.linkhub.oms.common.response.PageDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.util.cellwalk.CellWalkContext;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 用户账户模块
 *
 * @author Frank
 */
@Slf4j
@RestController
@Api(tags = {"物流运费模板"})
@RequestMapping("/logistic_freight/template")
@SaCheckLogin
public class LogisticFreightTemplateController {

    @Resource
    private LogisticFreightTemplateBsService logisticFreightTemplateBsService;

    @GetMapping()
    @ApiOperation(value = "运费模版列表")
    @SaCheckRole(value = {RoleConstant.EMPLOYEE})
    public DataResponse<PageDTO<LogisticFreightTemplateVO>> getLogisticFreightTemplatePage(@Validated LogisticFreightTemplateQueryParam queryParam) {
        return DataResponse.success(logisticFreightTemplateBsService.getLogisticFreightTemplatePage(queryParam));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "运费模板详情")
    @SaCheckRole(value = {RoleConstant.EMPLOYEE})
    public DataResponse<LogisticFreightTemplateDetailVO> getLogisticFreightTemplateDetail(@PathVariable("id") Long id) {
        return DataResponse.success(logisticFreightTemplateBsService.getLogisticFreightTemplateDetail(id));
    }

    @PostMapping()
    @ApiOperation(value = "新建运费模板")
    @SaCheckRole(value = {RoleConstant.EMPLOYEE})
    public DataResponse<Void> addLogisticFreightTemplate(@Valid @RequestBody LogisticFreightTemplateAddParam logisticFreightTemplateAddParam) {
        logisticFreightTemplateAddParam.setUserId(LhSessionContext.getUserId());
        logisticFreightTemplateBsService.addLogisticFreightTemplate(logisticFreightTemplateAddParam);
        return DataResponse.success();
    }

    @PutMapping()
    @ApiOperation(value = "编辑运费模板")
    @SaCheckRole(value = {RoleConstant.EMPLOYEE})
    public DataResponse<Void> updLogisticFreightTemplate(@Valid @RequestBody LogisticFreightTemplateUpdParam logisticFreightTemplateUpdParam) {
        logisticFreightTemplateUpdParam.setUserId(LhSessionContext.getUserId());
        logisticFreightTemplateBsService.updLogisticFreightTemplate(logisticFreightTemplateUpdParam);
        return DataResponse.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除运费模板")
    @SaCheckRole(value = {RoleConstant.EMPLOYEE})
    public DataResponse<Void> delLogisticFreightTemplate(@PathVariable("id") Long id) {
        logisticFreightTemplateBsService.delLogisticFreightTemplate(id);
        return DataResponse.success();
    }

    @PostMapping("/excel")
    @ApiOperation(value = "导入运费模板")
    @SaCheckRole(value = {RoleConstant.EMPLOYEE})
    public DataResponse<LogisticFreightTemplateExcelVO> importExcel(@RequestParam("file") MultipartFile file) {
        return DataResponse.success(logisticFreightTemplateBsService.importExcel(file, LhSessionContext.getUserId()));
    }

    @GetMapping("/country_partition")
    @ApiOperation(value = "查询国家分区")
    public DataResponse<List<String>> getCountryPartition(@RequestParam("countryCode") String countryCode) {
        return DataResponse.success(logisticFreightTemplateBsService.getCountryPartition(countryCode));
    }

}
